package cn.sxt.clinic.controller;

import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.*;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/visitList")
@Controller
public class visitListController {

    @Autowired
    private ICaseOrderService caseOrderService;

    @Autowired
    private ICaseOrderTmpService caseOrderTmpService;

    @Autowired
    private ICaseOrderTmpOneService caseOrderTmpOneService;

    @Autowired
    private ICaseOrderTmpTwoService caseOrderTmpTwoService;

    @Autowired
    private IDictionaryService dictionaryService;


    @RequestMapping("/visitListManager")
    public String visitListManager(){
        return "visitListManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1")Integer pageNum,
                                   @RequestParam(defaultValue = "10")Integer pageSize,
                                   String keyword){
        IPage<CaseOrder> page =new Page<>(pageNum,pageSize);
        QueryWrapper<CaseOrder> wrapper =new QueryWrapper<>();
        IPage<CaseOrder> result = caseOrderService.page(page, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return  map;
    }

    @RequestMapping("/listA")
    @ResponseBody
    public Map<String,Object> listA(@RequestParam(defaultValue = "1")Integer pageNum,
                                   @RequestParam(defaultValue = "10")Integer pageSize,
                                   String parentId){
        IPage<CaseOrderTmp> page =new Page<>(pageNum,pageSize);
        QueryWrapper<CaseOrderTmp> wrapper =new QueryWrapper<>();
        wrapper.eq("case_order_number",parentId);
        IPage<CaseOrderTmp> result = caseOrderTmpService.page(page, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return  map;
    }
    @RequestMapping("/listB")
    @ResponseBody
    public Map<String,Object> listB(@RequestParam(defaultValue = "1")Integer pageNum,
                                    @RequestParam(defaultValue = "10")Integer pageSize,
                                    String parentId){
        IPage<CaseOrderTmpOne> page =new Page<>(pageNum,pageSize);
        QueryWrapper<CaseOrderTmpOne> wrapper =new QueryWrapper<>();
        wrapper.eq("case_order_number",parentId);
        IPage<CaseOrderTmpOne> result = caseOrderTmpOneService.page(page, wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",result.getTotal());
        map.put("rows",result.getRecords());
        return  map;
    }
    @RequestMapping("/listC")
    @ResponseBody
    public Map<String,Object> listC(@RequestParam(defaultValue = "1")Integer pageNum,
                                    @RequestParam(defaultValue = "10")Integer pageSize,
                                    String parentId){
        QueryWrapper<CaseOrderTmpTwo> wrapper =new QueryWrapper<>();
        wrapper.eq("case_order_number",parentId);
        List<CaseOrderTmpTwo> list = caseOrderTmpTwoService.list(wrapper);
        for (CaseOrderTmpTwo caseOrderTmpTwo : list) {
            QueryWrapper<Dictionary> dictionaryWrapper = new QueryWrapper<>();
            dictionaryWrapper.eq("dictionary_id",caseOrderTmpTwo.getDictionaryId());
            Dictionary dictionary = dictionaryService.getOne(dictionaryWrapper);
            caseOrderTmpTwo.setTypeName(dictionary.getDictionaryName());

            QueryWrapper<Dictionary> dictionaryWrapperB =new QueryWrapper<>();
            dictionaryWrapperB.eq("dictionary_id",caseOrderTmpTwo.getSurchargeId());
            Dictionary dictionaryb = dictionaryService.getOne(dictionaryWrapperB);
            caseOrderTmpTwo.setSurchargeTypeName(dictionaryb.getDictionaryName());
        }
        System.out.println("fjkasdklfhasdkghfbdsakjl;bfgn dksl"+list);
        HashMap<String , Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("rows",list);
        return  map;
    }

}
